﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using SQLiteDictionary;

namespace DEMO_SQLiteDictionaryInterop_and_SQLiteEnum
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        SQLiteDictionaryInterop<string> _varCollection;
        ObservableCollection<SqliteKeyValuePar<string, string>> _collection;

        public MainWindow()
        {
            InitializeComponent();

            try
            {
                MyVars.DataBasePath = "Settings";
                _varCollection = new SQLiteDictionaryInterop<string>(SQLiteDictionaryStorage.LocalAppData, "DEMO_SQLiteDictionary\\NewVars");

                INIVars();
                INIEvents();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        public void INIVars()
        {
            _myTextBox.Text = MyVars.Text;
            _slider.Value = MyVars.SliderPosition;
            _myCheckBox.IsChecked = MyVars.CheckStatus;

            _txt_key.Text = "";
            _txt_Value.Text = "";

            LoadVars();
        }
        public void INIEvents()
        {
            _myCheckBox.Checked += delegate { MyVars.CheckStatus.Value = _myCheckBox.IsChecked.Value; };
            _myCheckBox.Unchecked += delegate { MyVars.CheckStatus.Value = _myCheckBox.IsChecked.Value; };
            _myTextBox.TextChanged += delegate { MyVars.Text.Value = _myTextBox.Text; };
            _slider.ValueChanged += delegate { MyVars.SliderPosition.Value = _slider.Value; };
            _btn_reload.Click += delegate { LoadVars(); };

            _listView.SelectionChanged += OnSelectionChanged;
            _btn_Save.Click += SaveNewVar;
        }

        private void SaveNewVar(object sender, RoutedEventArgs e)
        {
            string key = _txt_key.Text;
            string val = _txt_Value.Text;

            if (String.IsNullOrEmpty(key) || String.IsNullOrEmpty(val)) return;

            _varCollection.Set<string>(key, val);
            LoadVars();
        }

        private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            int x = _listView.SelectedIndex;
            if (x == -1) return;

            _txt_key.Text = _collection[x].Key;
            _txt_Value.Text = _collection[x].Value;
        }

        public void LoadVars()
        {
            _listView.ItemsSource = null;
            _collection = new ObservableCollection<SqliteKeyValuePar<string, string>>();
            foreach (var v in _varCollection.GetDictionary<string>())
                _collection.Add(v);
            _listView.ItemsSource = _collection;
        }
    }

    public class MyVars
    {
        private static string database;
        public static string DataBasePath
        { 
            get { return database; }
            set { database = value; }
        }

        public static SQLiteEnum<string, string> Text
        { get { return new SQLiteEnum<string, string>("TextBox", "Write some text", DataBasePath); } }
        public static SQLiteEnum<string, bool> CheckStatus
        { get { return new SQLiteEnum<string, bool>("CheckBox", false, DataBasePath); } }
        public static SQLiteEnum<string, double> SliderPosition
        { get { return new SQLiteEnum<string, double>("Slider", 50.0, DataBasePath); } }
    }
}
